package com.platform.os.util;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * @author KangJun 2007-9-26.
 */
@SuppressWarnings("unchecked")
public class Configure {
	static String configureFile;
	static String rootName;
	static HashMap<String, SysConfigureBean> map = new HashMap<String, SysConfigureBean>();

	/** 将配置文件中的信息读入到HashMap.
	 * @param realFile
	 * @return
	 */
	public static boolean initial(String realFile) {
		configureFile = realFile;
		map.clear();
		try {
			File file = new File(realFile);
			SAXReader reader = new SAXReader();
			Document document = (Document) reader.read(file);
			Element rootElement = document.getRootElement();
			rootName = rootElement.getName();
            Iterator it = rootElement.elementIterator();
            Element childElement;
            int order = 0;
            while (it.hasNext()) {
            	childElement = (Element) it.next();
    			String childName = childElement.getName();
            	Iterator it1 = childElement.elementIterator();
            	Element grandChildElement;
            	while (it1.hasNext()) {
            		grandChildElement = (Element) it1.next();
            		String grandChildName = grandChildElement.getName();
            		String keyName = childName + "." + grandChildName;
            		String keyPath = "/" + rootName + "/" + childName + "/" + grandChildName;
            		String keyValue = grandChildElement.getText();
            		String explain = grandChildElement.attributeValue("explain");
            		String keyValueRange = grandChildElement.attributeValue("valueRange");
            		SysConfigureBean bean = new SysConfigureBean();
            		bean.setKeyName(keyName);
            		bean.setExplain(explain);
            		bean.setKeyPath(keyPath);
            		bean.setKeyValue(keyValue);
            		bean.setKeyValueRange(keyValueRange);
            		bean.setOrder(order);
             		map.put(keyName, bean);
             		order++;
            	}
            }
            return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/** 获得某个参数的值.
	 * @param keyName 格式为parameter.parameterName
	 * @return
	 */
	public static String getKeyValue(String keyName) {
		if (map == null) {
			return null;
		}
		Object obj = map.get(keyName);
		if (obj == null) {
			return null;
		}
		SysConfigureBean bean = (SysConfigureBean) obj;
		return bean.getKeyValue();
	}

	/** 设置参数值，同时写入xml文件.
	 * @param keyName 格式为parameter.parameterName
	 * @param keyValue String
	 * @return
	 */
	public static boolean setKeyValue(String keyName, String keyValue) {
		if (map == null) {
			return false;
		}
		Object obj = map.get(keyName);
		if (obj == null) {
			return false;
		}
		SysConfigureBean bean = (SysConfigureBean) obj;
		if (!XmlHandle.setNode(configureFile, bean.getKeyPath(), keyValue)) {
			return false;
		}
		return initial(configureFile);
	}

	/** 增加参数，同时写入xml文件.
	 * @param keyName 格式为parameter.parameterName
	 * @param keyValue String
	 * @return
	 */
	public static boolean addKey(String keyName, String keyValue) {
		if (map == null) {
			return false;
		}
		int index = keyName.indexOf(".");
		if (index == -1) {
			return false;
		}
		String childName = keyName.substring(0, index);
		String grandChildName = keyName.substring(index + 1);
		String keyPath = "/" + rootName + "/" + childName + "/" + grandChildName;
		if (!XmlHandle.addNode(configureFile, keyPath, keyValue)) {
			return false;
		}
		return initial(configureFile);
	}

	/** 删除参数
	 * @param keyName 格式为parameter.parameterName
	 * @return
	 */
	public static boolean removeKey(String keyName) {
		if (map == null) {
			return false;
		}
		int index = keyName.indexOf(".");
		if (index == -1) {
			return false;
		}
		String childName = keyName.substring(0, index);
		String grandChildName = keyName.substring(index + 1);
		String keyPath = "/" + rootName + "/" + childName + "/" + grandChildName;
		if (!XmlHandle.removeNode(configureFile, keyPath)) {
			return false;
		}
		return initial(configureFile);
	}

	public static HashMap getMap() {
		return map;
	}
}
